<template>
  <el-dialog
      top="10vh"
      width="600px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'banner',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        image_url: '',
        link_url: '',
        location: 0,
        sort: 0,
        status: 1
      },
      rules: {
        image_url: [{required: true, message: '不能为空'}],
        location: [{required: true, message: '不能为空'}],
      },
      formList: [
        {
          label: '图片',
          prop: 'image_url',
          name: 'file',
          colSpan: 24
        },
        // {
        //   label: '位置',
        //   prop: 'location',
        //   name: 'select',
        //   colSpan: 24,
        //   options: [
        //     {
        //       value: 0,
        //       label: '首页'
        //     },
        //     {
        //       value: 1,
        //       label: '分类'
        //     }
        //   ]
        // },
        {
          label: '排序',
          prop: 'sort',
          name: 'inputNumber',
          colSpan: 24
        },
        {
          label: '状态',
          prop: 'status',
          name: 'switch',
          colSpan: 24,
          activeValue: 1,
          inactiveValue: 0
        },
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
